<?php


namespace App\Services;


use Illuminate\Support\Facades\Redis;
use AlibabaCloud\SDK\Dysmsapi\V20170525\Dysmsapi;
use \Exception;
use AlibabaCloud\Tea\Exception\TeaError;
use AlibabaCloud\Tea\Utils\Utils;

use Darabonba\OpenApi\Models\Config;
use AlibabaCloud\SDK\Dysmsapi\V20170525\Models\SendSmsRequest;
use AlibabaCloud\Tea\Utils\Utils\RuntimeOptions;
class AliyunSmsService
{

    const MOBILE_LIMIT = 30;
    const IP_LIMIT = 30;
    const SMS_SEND_TYPE_OFFLINE = 'offline';

    ////智讯云老接口的状态码
    protected static $fail_short_msg = array(
        '-1' => '账号未注册', '-2' => '其他错误', '-3' => '帐号或密码错误', '-5' => '余额不足，请充值', '-6' => '定时发送时间不是有效的时间格式',
        '-7' => '提交信息末尾未签名，请添加中文的企业签名【 】', '-8' => '发送内容需在1到300字之间', '-9' => '发送号码为空',
        '-10' => '定时时间不能小于系统当前时间', '-100' => 'IP黑名单', '-102' => '账号黑名单', '-103' => 'IP未导入白名单'
    );
    /**
     * 使用AK&SK初始化账号Client
     * @param string $accessKeyId
     * @param string $accessKeySecret
     * @return Dysmsapi Client
     */
    public static function createClient($accessKeyId, $accessKeySecret){
        $config = new Config([
            // 必填，您的 AccessKey ID
            "accessKeyId" => $accessKeyId,
            // 必填，您的 AccessKey Secret
            "accessKeySecret" => $accessKeySecret
        ]);
        // Endpoint 请参考 https://api.aliyun.com/product/Dysmsapi
        $config->endpoint = "dysmsapi.aliyuncs.com";
        return new Dysmsapi($config);
    }

    /**
     * @param string[] $args
     * @return void
     */
    public static function main($args){
        // 请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_ID 和 ALIBABA_CLOUD_ACCESS_KEY_SECRET。
        // 工程代码泄露可能会导致 AccessKey 泄露，并威胁账号下所有资源的安全性。以下代码示例使用环境变量获取 AccessKey 的方式进行调用，仅供参考，建议使用更安全的 STS 方式，更多鉴权访问方式请参见：https://help.aliyun.com/document_detail/311677.html
        $client = self::createClient(config('alipay.oss.accessKeyId'),config('alipay.oss.accessKeySecret'));
        $sendSmsRequest = new SendSmsRequest([
            "phoneNumbers" => "17779146889",
            "signName" => "都来租物",
            "templateCode" => "SMS_284975778",
            "templateParam" => "{\"code\":\"123456\"}"
        ]);
        try {
            // 复制代码运行请自行打印 API 的返回值
            $response=$client->sendSmsWithOptions($sendSmsRequest, new RuntimeOptions([]));
           return $response->statusCode;
        }
        catch (Exception $error) {
            if (!($error instanceof TeaError)) {
                $error = new TeaError([], $error->getMessage(), $error->getCode(), $error);
            }
            // 如有需要，请打印 error
            Utils::assertAsString($error->message);
        }
      }

}
